package com.itheima.tlias84.controller;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.config.StudentConfig;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/yml")
public class TestReadTmlController {
    //第一种
    //    @Value("${}") 不可复用，只能单个赋值
//    String name;
//    @Value("${student.age}")
//    Integer age;
//    @Value("${student.subject}")
//    String[] subjects;
//    @Value("${student.teachers}")
//    List<String> list;
    //第二种：可以使用StudentConfig代替↑
    @Resource
    StudentConfig config;
    //第三种
    @Resource
    Environment evn;

    @GetMapping
    public ResultUtil readCoustomConfigByYml() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("姓名", config.getName());
        jsonObject.put("年龄", config.getAge());
        jsonObject.put("科目", config.getSubjects());
        jsonObject.put("老师", config.getTeachers());
        jsonObject.put("家庭成员", config.getFamily());
        //第三种
        String[] activeProfiles = evn.getActiveProfiles();
        jsonObject.put("active",activeProfiles);
        String property = evn.getProperty("server.port");
        jsonObject.put("port",property);
        return ResultUtil.success(jsonObject);
    }
}
